﻿using DomainModel;
using MusicManager.Interfaces;
using MusicManager.BL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace AzureTestClient
{
    internal class Database
    {
        private const string DEFAULT_MUSICMANAGER_EP = "MusicManagerService_BasicHttpEndpoint";
        private const string DEFAULT_USERMANAGER_EP = "UserManagerService_BasicHttpEndpoint";

        private static void AddUser(IUser admin, User newUser)
        {
            try
            {
                if (admin.InsertUser(newUser))
                    Console.WriteLine("New User: \"" + newUser.Name + "\" inserted");
                else
                    Console.WriteLine("User already exists - Insert-Command failed");
            }
            catch (Exception ex)
            {
                Console.WriteLine("An exception occured in AzureTestClient.Database.AddUser");
            }
        }

        private static void AddSongForToken(IMusic admin, Music newMusic, string token)
        {
            try
            {
                if(admin.InsertMusic(newMusic, token))
                    Console.WriteLine("Song added sucessfully");
                else
                    Console.WriteLine("Problem occured with user / token");
            }
            catch (DuplicateMusicException dme)
            {
                Console.WriteLine("Duplicate Music insertion not possible");
            }
        }

        public static void Init()
        {
            using (ChannelFactory<IUser> cf = new ChannelFactory<IUser>(DEFAULT_USERMANAGER_EP))
            {
                Console.WriteLine("Endpoint address: address {0}", cf.Endpoint.Address.Uri);

                IUser uAdmin = cf.CreateChannel();

                AddUser(uAdmin, new User() { Id = 1, Name = "Luan", Password = "asdf1234" });
                AddUser(uAdmin, new User() { Id = 2, Name = "Michael", Password = "blabla123" });
                AddUser(uAdmin, new User() { Id = 3, Name = "Idiot", Password = "cakeisalie" });
            }
        }
    }
}
